﻿/**
 * 生产工艺Js文件
 * dai 2024.03.13
 * **/
var showTab = "";//当前展示工艺tab编码
/**
 * 加载工艺关联工序选项卡数据
 * data:工序数据
 * divId:容器id
 * isdel:删除工序权限
 * **/
function loadGxTab(data,divId,isdel) {
    var uTitle = '';
    var uContents = '';
    var cCss = "";
    let tabClass = 'class="layui-this"';
    for (var i = 0; i < data.length; i++) {
     
        if (i > 0) {
            tabClass = "";
        }
        uTitle += '<li ' + tabClass + ' lay-id="' + data[i].cCode + '">' + data[i].cName;
        if (isdel) {
            uTitle += '<span style="margin-left:3%;" onclick="DelTab(\'' + data[i].cCode + '\')"><i class="layui-icon">&#x1006;</i></span>';
        }
        uTitle += "</li>";
        var divCode = "div" + data[i].cCode;
        if (i == 0) {
            cCss = "layui-tab-item layui-show"
            showTab = data[i].cCode;
            $("input[name='gxCode']").val(showTab);
        } else {
            cCss = "layui-tab-item";
        }
        var btnStr = '<div class="layui-inline layui-show-xs-block"><button class="layui-btn layui-btn-sm" type="button"' +
            ' onclick="SaveFormData(\'' + data[i].cCode + '\')"><i class="layui-icon layui-icon-edit"></i>' +
        '</button></div> ';
        uContents += '<div id="cd' + data[i].cCode + '" class="' + cCss + '">' +
            btnStr + '<form id="f' + data[i].cCode + '" class="layui-form"><div id="' + divCode + '">' +
            data[i].cName + '</div></form></div>';
    }
    var contents = '<div class="layui-tab" lay-filter="gyTab" ><ul class="layui-tab-title" >' + uTitle + '</ul>' +
        '<div class="layui-tab-content">' + uContents + '</div></div>';
    $('#' + divId).empty();
    $('#' + divId).append(contents);
}
/**
 * 加载工艺工序表单
 * gxCode:关联工序
 * proCode:关联存货(或大类)
 * divId:容器id
 **/
function loadGyForm(gxCode, proCode, divId) {
    var json = {
        gxCode: gxCode,
        proCode: proCode
    }
    var msg = JSON.stringify(json);
    $.ajax({
        url: "../ProductSet/loadGxFormByCode",
        data: { datas: msg },
        dataType: "json",
        type: "POST",
        success: function (rsl) {
            if (rsl.result) {
                InitDataForm(rsl.data, divId);
                GetFormValue();
            } else {
                layer.msg(rsl.message);
            }
        }
    });
}
/**
 * 生产计划工艺删除
 * tabId：工序id
 * plCode：关联计划编码
 **/
function DelTab(tabId, plCode) {
    layer.confirm('确认要删除选中工序？', { icon: 3 }, function () {
        element = layui.element;
        $.ajax({
            url: "../Plan/DelPlanGongYi",
            data: { plCode: plCode, gxCode: tabId },
            dataType: "json",
            type: "POST",
            success: function (rsl) {
                if (rsl.result) {
                    element.tabDelete('gyTab', tabId); //删除Tab
                    layer.msg('工序删除成功', { icon: 1 });
                } else {
                    layer.msg(rsl.message);
                }
            }
        });
        
    }, function () {

    });
}

/**
 * 设置表单数据数据
 * **/
function SetStandardValue(sData) {
    $.each(sData[0], function (name, value) {
        if (name != "gxCode" && name != "relationType") {
            $("input[ name='" + name + "']").val(value);
        }
    });
    for (var i = 0; i < sData.length; i++) {
        if (sData[i].fieldType == "fxlk") {
            $("#" + sData[i].fieldCode + " option[value='" + sData[i].value + "'] ").attr("selected", true);
        } else if (sData[i].fieldType == "ftext") {
            $("input[ name='" + sData[i].fieldCode + "']").val(sData[i].value);
        }
    }
    layui.use('form', function () {
        var form = layui.form;
        form.render();
    });
}